package com.naiterui.ehp.bs.weixin.controller.inner;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BaseExceptionCodes;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bs.weixin.service.IAccessTokenService;
import com.naiterui.ehp.bs.weixin.wechat.WeChatApiUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author luao
 * @date 2022/4/21 10:38 AM
 **/
@Api(tags = {SwaggerTag.WEIXIN + SwaggerTag.INNER_API + SwaggerTag.SELF_INSPECT})
@RestController
@RequestMapping("weixin/inner/self/inspect")
@RequiredArgsConstructor
public class InnerSelfInspectController {

    private final IAccessTokenService accessTokenService;

    @Value("${ehp.domain.img}")
    private String imgHost;

    @ApiOperation(value = "微信小程序自检")
    @GetMapping("/wx/ma/check")
    public void wxMaCheck() throws BusinessException {
        String accessToken = accessTokenService.accessToken();
        if (accessToken == null) {
            throw new BusinessException(BaseExceptionCodes.FAILED, "微信小程序自检失败");
        }
    }

    @ApiOperation(value = "微信小程序二维码")
    @GetMapping("/wx/ma/qrcode")
    public String miniappQrcode() throws Exception {
        String accessToken = accessTokenService.accessToken();
        String qrCode = WeChatApiUtil.createQrCode(accessToken, ConfigUtil.getString("wx.safe-inspect-pay-path"), 430);
        return imgHost + qrCode;
    }
}
